/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The software in this package is published under the terms of the GPL v2.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE-GPL.txt file.
 */

package org.cemantika.aspects.logger;

import org.cemantika.util.CemantikaLogger;
import org.drools.event.process.ProcessCompletedEvent;
import org.drools.event.process.ProcessEventListener;
import org.drools.event.process.ProcessStartedEvent;

/**
 * ContextualBehaviorLogger represents the aspect that writes in the log information about the
 * execution of the {@link ContextualBehavior} class.
 * 
 * @author andersonfellipe
 */
public aspect ContextualBehaviorLogger {

    /**
     * Represents the initiation of the execution of a contextual graph.
     * 
     * @param event the object of the event {@link ProcessStartedEvent}.
     */
    pointcut processStarted(ProcessStartedEvent event): 
        execution(void ProcessEventListener.beforeProcessStarted(ProcessStartedEvent)) && 
        args(event);

    /**
     * Represents when the contextual graph completes it's execution.
     * 
     * @param event the object of the event {@link ProcessCompletedEvent}.
     */
    pointcut processCompleted(ProcessCompletedEvent event): 
        execution(void ProcessEventListener.afterProcessCompleted(ProcessCompletedEvent)) && 
        args(event);

    /**
     * Represents the after advice that writes in the log when the contextual graph starts it's
     * execution.
     * 
     * @param event the object of the event {@link ProcessStartedEvent}.
     */
    @SuppressWarnings("restriction")
    after(ProcessStartedEvent event): processStarted(event) {
        CemantikaLogger.logger.info("** Contextual graph execution started: "
                + event.getProcessInstance().getProcessId());
    }

    /**
     * Represents the after advice that write in the log when the contextual graph completes it's
     * execution.
     * 
     * @param event the object of the event {@link ProcessCompletedEvent}.
     */
    @SuppressWarnings("restriction")
    after(ProcessCompletedEvent event): processCompleted(event) {
        CemantikaLogger.logger.info("** Contextual graph execution completed: "
                + event.getProcessInstance().getProcessId());
    }

}
